package com.fzshang.shopping.api.controller;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.digest.Digester;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzshang.shopping.commen.bean.Journal;
import com.fzshang.shopping.commen.bean.Sale;
import com.fzshang.shopping.commen.bean.UserInfo;
import com.fzshang.shopping.commen.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @AUTHOR 冯政尚
 * @DATE 2020/3/2 20:31
 * @DAY_NAME_FULL: 星期一
 */
@Controller
@RequestMapping(value = "admin/index")
public class IndexController extends BaseController {

    /**
     * @return 登陆
     */
    @RequestMapping(value = "login.html")
    public String login(){
        return "admin/login";
    }
    /**
     * @return 首页
     */
    @RequestMapping(value = "index.html")
    public String index(Model model){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",1);
        model.addAttribute("sum",userInfoService.list(queryWrapper).size());
        model.addAttribute("saleSum",saleService.list().size());
        model.addAttribute("wxCount",userService.list().size());
        return "admin/index";
    }
    /**
     * @return 欢迎页
     */
    @RequestMapping(value = "welcome.html")
    public String welcome(Model model){
        //查询总收入
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("state",2);
        List<UserInfo> userInfos = userInfoService.list(queryWrapper1);
        for (int i = 0; i < userInfos.size(); i++) {
            userInfos.get(i).setMm(Double.parseDouble(userInfos.get(i).getMoney()));
        }
       Double count = userInfos.stream().collect(Collectors.summingDouble(UserInfo::getMm));
        List<Sale> sales = saleService.list();
        for (int i = 0; i < sales.size(); i++) {
            sales.get(i).setMm(Double.parseDouble(sales.get(i).getMoney()));
        }
        Double count1 = sales.stream().collect(Collectors.summingDouble(Sale::getMm));
        BigDecimal bigDecimal = new BigDecimal(count);
        BigDecimal bigDecimal1 = new BigDecimal(count1);
        Double bigDecimal2 = bigDecimal.add(bigDecimal1).doubleValue();
        model.addAttribute("money",String.format("%.2f", bigDecimal2));
        //待发货
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",4);
        model.addAttribute("daiFHuo",shoppingOrderService.list(queryWrapper).size());
        //待收货
        QueryWrapper queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("state",6);
        model.addAttribute("daiShouHuo",shoppingOrderService.list(queryWrapper2).size());
        //已完成
        QueryWrapper queryWrapper3 = new QueryWrapper();
        queryWrapper3.eq("state",7);
        model.addAttribute("yiWanCheng",shoppingOrderService.list(queryWrapper3).size());
        //已完成
        QueryWrapper queryWrapper4 = new QueryWrapper();
        queryWrapper4.eq("state",3);
        model.addAttribute("tuiShou",shoppingOrderService.list(queryWrapper4).size());
        return "admin/welcome";
    }



    @RequestMapping(value = "getIpNameUrl")
    @ResponseBody
    public void getIpNameUrl(String pathName,String protocol,String browser,String edition,String operating) throws UnknownHostException {
        Journal journal = new Journal();
        journal.setIp(InetAddress.getLocalHost().getHostAddress());
        journal.setName(InetAddress.getLocalHost().getHostName());
        journal.setCreateTime(System.currentTimeMillis());
        journal.setUserName(getUserInfo().getAccount());
        journal.setUrl(pathName);
        journal.setAgreement(protocol);
        journal.setBrowser(browser);
        journal.setEdition(edition);
        journal.setOperating(operating);
        journalService.save(journal);
    }


//    public static boolean md5(){
//        String testStr = "123456";
//        String md5Hex1 = DigestUtil.md5Hex(testStr);
//        System.out.println("加密之后:"+md5Hex1);
//        String a = "123456";
//        String md51 = DigestUtil.md5Hex(a);
//        if(md5Hex1.equals(md51)){
//            System.out.println("一样");
//            return true;
//        }
//        System.out.println("不一样");
//        return false;
//    }
//
//    public static void main(String[] args) {
//        md5();
//    }
//
//
//
//    @RequestMapping(value = "vue.html")
//    public String vue(){
//        return "admin/vue";
//    }

}
